<?php
class Administrator_ProductController extends AdministratorController implements IController{
	/**
	 * Product_Model
	 * @var Administrator_Product_Model
	 */
	protected $_model;	
	
	
	/* (non-PHPdoc)
	 * @see AdministratorController::indexAction()
	 */
	public function indexAction() {
		$this->view()->productCount = $this->_model->countAllProduct();
		$this->view()->productCatCount = $this->_model->countAllProductCat();
		$this->view()->productDisplayCount = $this->_model->countAllProductDisplay();
		$this->view()->productDistributorCount = $this->_model->countAllProductDistributor();
		$this->view()->productManufactureCount = $this->_model->countAllProductManufacture();
		$this->view()->productTypeCount = $this->_model->countAllProductType();
		$this->view()->top10New = $this->_model->getTopNewestProduct($this->getContext());
	}

	public function initData() {
		$this->view()->viewBag['productCat'] = $this->_model->getProductCatForCombo();
		$this->view()->viewBag['productType'] = $this->_model->getProductTypeForCombo();
		$this->view()->viewBag['productDisplay'] = $this->_model->getProductDisplayForCombo();
		$this->view()->viewBag['productManufacture'] = $this->_model->getProductManufactureForCombo();
	}
	
	/* (non-PHPdoc)
	 * @see AdministratorController::initSearch()
	 */
	public function initSearch() {
		$this->view()->viewBag['productCat'] = ArrayUltils::insertAtFirstCombo($this->_model->getProductCatForCombo());
		$this->view()->viewBag['productType'] = ArrayUltils::insertAtFirstCombo($this->_model->getProductTypeForCombo());
		$this->view()->viewBag['productDisplay'] = ArrayUltils::insertAtFirstCombo($this->_model->getProductDisplayForCombo());
		$this->view()->viewBag['productManufacture'] = ArrayUltils::insertAtFirstCombo($this->_model->getProductManufactureForCombo());
	}

	public function validateData($data) {
		$result = array();
		$errors = array();
		if($data['name'] == null ||$data['name'] == ""){
			array_push($errors, "Bạn phải nhập tên");
		}
		if($data['code'] == null ||$data['code'] == ""){
			array_push($errors, "Bạn phải nhập mã sản phẩm");
		}
		if($data['price1'] == null ||$data['price1'] == ""){
			array_push($errors, "Bạn phải nhập giá căn bản");
		}
		if(count($errors)>0){
			$result['isError'] = true;
			$result['errors'] = $errors;
		}
		else {
			$result['isError'] = false;
			$result['errors'] = null;
		}
		$result['entity'] = $data;
		return $result;
	}
	/* (non-PHPdoc)
	 * @see AdministratorController::getSearchQuery()
	*/
	protected function getSearchQuery($se) {
		$sql = 'SELECT p.id, p.name,p.code,p.image,p.price1, p.hit,
				c.name as '.Product::CATNAME.',
				t.name as '.Product::TYPENAME.',
				m.name as '.Product::MANUFACTURENAME.',
				s.name as '.Product::STATUSNAME.'
				FROM product p
				LEFT JOIN productcat as c on p.productcat = c.id
				LEFT JOIN producttype as t on p.producttype = t.id
				LEFT JOIN productmanufacture as m on p.manufacture = m.id
				LEFT JOIN status as s on p.status = s.id
				 where p.id != 0 ';
	 	if(isset($se[Product::ID])&& $se[Product::ID] != "")
			$sql .= " AND p." . Product::ID ."='".$se[Product::ID]."'";
		if(isset($se[Product::CODE])&& $se[Product::CODE] != "")
			$sql .= " AND p." . Product::CODE ." like '%".$se[Product::CODE]."%'";
		if(isset($se[Product::NAME])&& $se[Product::NAME] != "")
			$sql .= " AND p." . Product::NAME ." like '%".$se[Product::NAME]."%'";
		
		if(isset($se[Product::PRODUCTCAT])&& $se[Product::PRODUCTCAT] != "")
			$sql .= " AND p." . Product::PRODUCTCAT ."='".$se[Product::PRODUCTCAT]."'";
		if(isset($se[Product::PRODUCTTYPE])&& $se[Product::PRODUCTTYPE] != "")
			$sql .= " AND p." . Product::PRODUCTTYPE ."='".$se[Product::PRODUCTTYPE]."'";
		if(isset($se[Product::MANUFACTURE])&& $se[Product::MANUFACTURE] != "")
			$sql .= " AND p." . Product::MANUFACTURE ."='".$se[Product::MANUFACTURE]."'";
		if(isset($se[Product::PRODUCTDISPLAY])&& $se[Product::PRODUCTDISPLAY] != "")
			$sql .= " AND p." . Product::PRODUCTDISPLAY ."='".$se[Product::PRODUCTDISPLAY]."'";
		if(isset($se[Product::PRICE1])&& $se[Product::PRICE1] != "")
			$sql .= " AND p." . Product::PRICE1 ."='".$se[Product::PRICE1]."'";
		if(isset($se[Constant::ORDERBY])){
			$sql .=" order by p." . $se[Constant::ORDERBY];
			if(isset($se[Constant::ORDERAS]))
				$sql .= " " . $se[Constant::ORDERAS];
			else
				$sql .= " ASC";
		}
		$page = $se[Constant::PAGE];
		$size = $se[Constant::SIZE];
		$sql .= " limit " . (($page - 1) * $size) . "," . $size;
		return $sql;
	}
	
	/* (non-PHPdoc)
	 * @see AdministratorController::getCountQuery()
	*/
	protected function getCountQuery($se) {
		$sql = 'SELECT count(p.id) as '.Constant::TOTAL.' FROM product p where p.id != 0';
		if(isset($se[Product::ID])&& $se[Product::ID] != "")
			$sql .= " AND p." . Product::ID ."='".$se[Product::ID]."'";
		if(isset($se[Product::CODE])&& $se[Product::CODE] != "")
			$sql .= " AND p." . Product::CODE ." like '%".$se[Product::CODE]."%'";
		if(isset($se[Product::NAME])&& $se[Product::NAME] != "")
			$sql .= " AND p." . Product::NAME ." like '%".$se[Product::NAME]."%'";
		
		if(isset($se[Product::PRODUCTCAT])&& $se[Product::PRODUCTCAT] != "")
			$sql .= " AND p." . Product::PRODUCTCAT ."='".$se[Product::PRODUCTCAT]."'";
		if(isset($se[Product::PRODUCTTYPE])&& $se[Product::PRODUCTTYPE] != "")
			$sql .= " AND p." . Product::PRODUCTTYPE ."='".$se[Product::PRODUCTTYPE]."'";
		if(isset($se[Product::MANUFACTURE])&& $se[Product::MANUFACTURE] != "")
			$sql .= " AND p." . Product::MANUFACTURE ."='".$se[Product::MANUFACTURE]."'";
		if(isset($se[Product::PRODUCTDISPLAY])&& $se[Product::PRODUCTDISPLAY] != "")
			$sql .= " AND p." . Product::PRODUCTDISPLAY ."='".$se[Product::PRODUCTDISPLAY]."'";
		if(isset($se[Product::PRICE1])&& $se[Product::PRICE1] != "")
			$sql .= " AND p." . Product::PRICE1 ."='".$se[Product::PRICE1]."'";
		return $sql;
	}
	
	/* (non-PHPdoc)
	 * @see AdministratorController::serializeSearchEntity()
	*/
	protected function serializeSearchEntity() {
		$se = parent::serializeSearchEntity();
		if(!isset($se[Constant::ORDERBY]))
			$se[Constant::ORDERBY] = Product::NAME;
		return $se;
	}	
	public function beforeSave($obj) {
		$product = $this->_model->getByID($obj);
		if(isset($_FILES['Product_image']) && $_FILES['Product_image']["size"]>0){
			$imageName = $obj[Product::NAME] + $obj[Product::ID];
			$obj[Product::IMAGE] = $this->uploadImage(Product::IMAGE, $imageName);
		}
		else{
			if(isset($product))
				$obj[Product::IMAGE] = $product[Product::IMAGE];
		}
		return $obj;
	}
	/* (non-PHPdoc)
	 * @see AdministratorController::createNewObject()
	 */
	public function createNewObject() {
		$obj = parent::createNewObject();
		$obj[Product::PRODUCTDISPLAY] = 4;
		$obj[Product::ORDINAL] = 16;
		return $obj;
	}
}